package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.FamilyMemberElderDto;
import com.zzyl.nursing.dto.QueryDeviceDataListDto;
import com.zzyl.nursing.dto.QueryDeviceDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 老人家属Controller
 *
 * @author ruoyi
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属的接口")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;


    @PostMapping("/login")
    @ApiOperation("小程序登录")
    public AjaxResult login(@RequestBody UserLoginRequestDto dto) {
        LoginVo loginVo = familyMemberService.login(dto);
        return success(loginVo);
    }

    /**
     * 绑定家人
     * @param dto 绑定信息
     * @param token 登录token
     * @return 绑定结果
     */
    @PostMapping("/add")
    @ApiOperation("绑定家人")
    public AjaxResult add(@RequestBody FamilyMemberElderDto dto,
                          @RequestHeader(value = "Authorization", required = false) String token) {
        int i = familyMemberService.add(dto, token);
        return success(i);
    }

    /**
     * 解绑老人
     * @param id 老人id
     * @return 删除结果
     */
    @DeleteMapping("/deleteById")
    @ApiOperation("解绑老人")
    public AjaxResult delete(Long id) {
        int i = familyMemberElderMapper.deleteFamilyMemberElderById(id);
        return success(i);
    }
    /**
     * 获取家人列表
     * @param token 登录token
     * @return 家人列表
     */
    @GetMapping("/my")
    @ApiOperation("获取家人列表")
    public AjaxResult my(@RequestHeader(value = "Authorization", required = false) String token) {
        List<FamilyMemberElder> familyMemberElderList = familyMemberService.selectFamilyMember(token);

        return success(familyMemberElderList);
    }

    /**
     * 查询老人家属列表
     * @param pageNum 页码
     * @param pageSize 页大小
     * @param token 登录token
     * @return 老人家属列表
     */
    @GetMapping("/list-by-page")
    @ApiOperation("查询老人家属列表")
    public AjaxResult list(int pageNum, int pageSize, @RequestHeader(value = "Authorization", required = false) String token) {
        List<FamilyMemberElderVo> list = familyMemberService.pageList(pageNum, pageSize, token);
        return success(list);
    }

    /**
     * 查询健康数据
     * @param dto 查询条件
     * @param token 登录token
     * @return 设备属性
     */
    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation("查询健康数据")
    public R<QueryDeviceVo> queryDevicePropertyStatus(@RequestBody QueryDeviceDto dto,
                                                      @RequestHeader(value = "Authorization", required = false) String token) {
        QueryDeviceVo result = familyMemberService.queryDevicePropertyStatus(dto, token);
        return R.ok(result);
    }

    /**
     * 按天统计查询指标数据
     * @param functionId 功能ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param iotId 设备ID
     * @return 指标数据
     */
    @GetMapping("/queryDeviceDataListByDay")
    @ApiOperation("按天统计查询指标数据")
    public DeviceDataVo queryDeviceDataListByDay(
            @RequestParam("functionId") String functionId,
            @RequestParam("startTime") Long startTime,
            @RequestParam("endTime") Long endTime,
            @RequestParam("iotId") String iotId) {
        QueryDeviceDataListDto dto = new QueryDeviceDataListDto();
        dto.setFunctionId(functionId);
        dto.setStartTime(startTime);
        dto.setEndTime(endTime);
        dto.setIotId(iotId);
        List<QueryDeviceDataListVo> result = familyMemberService.queryDeviceDataListByDay(dto);
        return new DeviceDataVo(
                "200",
                "成功",
                result,
                LocalDateTime.now()
        );
    }

    /**
     * 按周统计查询指标数据
     * @param functionId 功能ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param iotId 设备ID
     * @return 指标数据
     **/
    @GetMapping("/queryDeviceDataListByWeek")
    @ApiOperation("按周统计查询指标数据")
    public DeviceDataVo queryDeviceDataListByWeek(
            @RequestParam("functionId") String functionId,
            @RequestParam("startTime") Long startTime,
            @RequestParam("endTime") Long endTime,
            @RequestParam("iotId") String iotId) {
        QueryDeviceDataListDto dto = new QueryDeviceDataListDto();
        dto.setFunctionId(functionId);
        dto.setStartTime(startTime);
        dto.setEndTime(endTime);
        dto.setIotId(iotId);
        List<QueryDeviceDataListVo> result = familyMemberService.queryDeviceDataListByWeek(dto);
        return new DeviceDataVo(
                "200",
                "成功",
                result,
                LocalDateTime.now()
        );
    }
}
